﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Register.Model;
using Register.IDAL;
using Register.DBUtility;
using Register.Command;
using System.Collections.Generic;
using System.Linq;
namespace Register.DAL
{
    /// <summary>
    /// 数据访问类:hospitalInfo
    /// </summary>
    public class HospitalInfoDAL : IHospitalInfoDAL
    {

        private string tableName = "System_hospitalInfo";
        #region 添加医院
        public  int  AddHospitalInfo(Model.HospitalInfo model)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into System_hospitalInfo(");
            strSql.Append("Hospital_ID,City_ID,Hospital_No,Hospital_Name,Hospital_Level,Hospital_Nature,Hospital_Type,Hospital_Address,Hospital_Phone,Hospital_Website,Hospital_Introduce,Hospital_Logo,Hospital_AddTime,Hospital_Recoder,Hospital_LastAmendTime,Hospital_LastAmendPerson,Hospital_State)");
            strSql.Append(" values (");
            strSql.Append("@Hospital_ID,@City_ID,@Hospital_No,@Hospital_Name,@Hospital_Level,@Hospital_Nature,@Hospital_Type,@Hospital_Address,@Hospital_Phone,@Hospital_Website,@Hospital_Introduce,@Hospital_Logo,@Hospital_AddTime,@Hospital_Recoder,@Hospital_LastAmendTime,@Hospital_LastAmendPerson,@Hospital_State)");
            SqlParameter[] parameters = {
					new SqlParameter("@Hospital_ID", SqlDbType.Char,5),
					new SqlParameter("@City_ID", SqlDbType.Char,5),
					new SqlParameter("@Hospital_No", SqlDbType.VarChar,12),
					new SqlParameter("@Hospital_Name", SqlDbType.VarChar,30),
					new SqlParameter("@Hospital_Level", SqlDbType.Char,4),
					new SqlParameter("@Hospital_Nature", SqlDbType.Char,4),
					new SqlParameter("@Hospital_Type", SqlDbType.Char,4),
					new SqlParameter("@Hospital_Address", SqlDbType.VarChar,100),
					new SqlParameter("@Hospital_Phone", SqlDbType.VarChar,20),
					new SqlParameter("@Hospital_Website", SqlDbType.VarChar,50),
					new SqlParameter("@Hospital_Introduce", SqlDbType.Text),
					new SqlParameter("@Hospital_Logo", SqlDbType.VarChar,50),
					new SqlParameter("@Hospital_AddTime", SqlDbType.DateTime),
					new SqlParameter("@Hospital_Recoder", SqlDbType.VarChar,20),
					new SqlParameter("@Hospital_LastAmendTime", SqlDbType.DateTime),
					new SqlParameter("@Hospital_LastAmendPerson", SqlDbType.VarChar,20),
					new SqlParameter("@Hospital_State", SqlDbType.Char,1)};
            parameters[0].Value = model.Hospital_ID;
            parameters[1].Value = model.City_ID;
            parameters[2].Value = model.Hospital_No;
            parameters[3].Value = model.Hospital_Name;
            parameters[4].Value = model.Hospital_Level;
            parameters[5].Value = model.Hospital_Nature;
            parameters[6].Value = model.Hospital_Type;
            parameters[7].Value = model.Hospital_Address;
            parameters[8].Value = model.Hospital_Phone;
            parameters[9].Value = model.Hospital_Website;
            parameters[10].Value = model.Hospital_Introduce;
            parameters[11].Value = model.Hospital_Logo;
            parameters[12].Value = model.Hospital_AddTime;
            parameters[13].Value = model.Hospital_Recoder;
            parameters[14].Value = model.Hospital_LastAmendTime;
            parameters[15].Value = model.Hospital_LastAmendPerson;
            parameters[16].Value = model.Hospital_State;

            return (int)DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
      
        }
        #endregion
        #region 删除医院
        public int DeleteHospitalInfo(String hospitalID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from System_hospitalInfo ");
            strSql.Append(" where Hospital_ID=@Hospital_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@Hospital_ID", SqlDbType.Char,5)};
            parameters[0].Value = hospitalID;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        #endregion
        #region 更新医院
       public int UpdateAdminInfo(HospitalInfo model){
           StringBuilder strSql = new StringBuilder();
           strSql.Append("update System_hospitalInfo set ");
           strSql.Append("Hospital_Name=@Hospital_Name,");
           strSql.Append("Hospital_Level=@Hospital_Level, ");
           strSql.Append("Hospital_Nature=@Hospital_Nature,");
           strSql.Append("Hospital_Type=@Hospital_Type, ");
           strSql.Append("Hospital_Address=@Hospital_Address, ");
           strSql.Append("Hospital_Phone=@Hospital_Phone, ");
           strSql.Append("Hospital_Website=@Hospital_Website, ");
           strSql.Append("Hospital_Introduce=@Hospital_Introduce, ");
           strSql.Append("Hospital_Logo=@Hospital_Logo, ");
           strSql.Append("Hospital_LastAmendPerson=@Hospital_LastAmendPerson, ");
           strSql.Append("Hospital_LastAmendTime=@Hospital_LastAmendTime");
           strSql.Append(" where Hospital_ID=@Hospital_ID");
           SqlParameter[] parameters = {
					new SqlParameter("@Hospital_Name", SqlDbType.VarChar,30),
					new SqlParameter("@Hospital_Level", SqlDbType.Char,4),
					new SqlParameter("@Hospital_Nature", SqlDbType.Char,4),
					new SqlParameter("@Hospital_Type", SqlDbType.Char,4),
					new SqlParameter("@Hospital_Address", SqlDbType.VarChar,100),
					new SqlParameter("@Hospital_Phone", SqlDbType.VarChar,20),
					new SqlParameter("@Hospital_Website", SqlDbType.VarChar,50),
					new SqlParameter("@Hospital_Introduce", SqlDbType.Text),
					new SqlParameter("@Hospital_Logo", SqlDbType.VarChar,50),
                    new SqlParameter("@Hospital_LastAmendPerson", SqlDbType.VarChar,20),
					new SqlParameter("@Hospital_LastAmendTime", SqlDbType.DateTime),
                    new SqlParameter("@Hospital_ID", SqlDbType.Char,5)
                                       };
          
       
           parameters[0].Value = model.Hospital_Name;
           parameters[1].Value = model.Hospital_Level;
           parameters[2].Value = model.Hospital_Nature;
           parameters[3].Value = model.Hospital_Type;
           parameters[4].Value = model.Hospital_Address;
           parameters[5].Value = model.Hospital_Phone;
           parameters[6].Value = model.Hospital_Website;
           parameters[7].Value = model.Hospital_Introduce;
           parameters[8].Value = model.Hospital_Logo;
           parameters[9].Value = model.Hospital_LastAmendPerson;
           parameters[10].Value = model.Hospital_LastAmendTime;
           parameters[11].Value = model.Hospital_ID;

           return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        #endregion
        

        public   List<Model.HospitalInfo> GetPagedHospitalInfo(int size, int pageIndex, string where)
        {
            return DataSetToList(DbHelperSQL.getPagedData(" * ", "Hospital_AddTime", where, tableName, OrderBy.DESC, pageIndex, size));
        }
        /// <summary>
        /// 根据参数ID获取参数名
        /// </summary>
        /// <param name="SystemParameter_ID"></param>
        /// <returns></returns>
        public string GetParamNameById(string SystemParameter_ID) {
            string strSql = "select  *   from System_parameter where SystemParameter_ID='"+SystemParameter_ID+"'";
            DataSet ds=DbHelperSQL.Query(strSql);
            string SystemParameter_Name = "";
            if (ds != null)
            {
                SystemParameter_Name = ds.Tables[0].Rows[0]["SystemParameter_Name"].ToString();

            }
            return SystemParameter_Name;
        }

        public bool Exists(string hospitalID)
        {
            string strSql = "select  HospitalInstall_ID  from System_hospitalInfo where Hospital_ID='" + hospitalID + "'";
            DataSet ds = DbHelperSQL.Query(strSql);

            if (ds != null)
            {    //存在记录
                if (ds.Tables[0].Rows[0]["HospitalInstall_ID"].ToString() != null && !String.IsNullOrEmpty(ds.Tables[0].Rows[0]["HospitalInstall_ID"].ToString()))
                {
                    return true;
                }

                else { return false; }

            }
            else
            { return false; }
        }
        /*
        public bool Exists(string Administrator_Name)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from System_adminInfo");
            strSql.Append(" where Administrator_Name=@Administrator_Name ");
            SqlParameter[] parameters = {
					new SqlParameter("@Administrator_Name", SqlDbType.VarChar)};
            parameters[0].Value = Administrator_Name;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        */
        public  Model.HospitalInfo GetHospitalInfo(string HospitalID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Hospital_ID,City_ID,Hospital_Address,Hospital_AddTime,Hospital_Introduce,Hospital_LastAmendPerson,Hospital_LastAmendTime,Hospital_Level,Hospital_Logo,Hospital_Name,Hospital_Nature,Hospital_No,Hospital_Phone,Hospital_Recoder,Hospital_State,Hospital_Type,Hospital_Website");
            strSql.Append(" where Hospital_ID=@Hospital_ID");
            SqlParameter[] parameters = {
					new SqlParameter("@Hospital_ID", SqlDbType.Char,5)};
            parameters[0].Value = HospitalID;
            return DataSetToList(DbHelperSQL.Query(strSql.ToString(), parameters)).First();
        }

        private List<Model.HospitalInfo> DataSetToList(DataSet ds)
        {
            List<Model.HospitalInfo> list = new List<Model.HospitalInfo>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                HospitalInfo hospital = new HospitalInfo();
                hospital.Hospital_ID = (string)dr["Hospital_ID"];
                hospital.City_ID = (string)dr["City_ID"];
                hospital.Hospital_Address = (string)dr["Hospital_Address"];
                hospital.Hospital_AddTime = (DateTime)dr["Hospital_AddTime"];
                hospital.Hospital_Introduce = (string)dr["Hospital_Introduce"];
                hospital.Hospital_LastAmendPerson = (string)dr["Hospital_LastAmendPerson"];
                hospital.Hospital_LastAmendTime = (DateTime)dr["Hospital_LastAmendTime"];
                hospital.Hospital_Level = (string)dr["Hospital_Level"];
                hospital.Hospital_Logo = (string)dr["Hospital_Logo"];
                hospital.Hospital_Name = (string)dr["Hospital_Name"];
                hospital.Hospital_Nature = (string)dr["Hospital_Nature"];
                hospital.Hospital_No = (string)dr["Hospital_No"];
                hospital.Hospital_Phone = (string)dr["Hospital_Phone"];
                hospital.Hospital_Recoder = (string)dr["Hospital_Recoder"];
                hospital.Hospital_State = (string)dr["Hospital_State"];
                hospital.Hospital_Type = (string)dr["Hospital_Type"];
                hospital.Hospital_Website = (string)dr["Hospital_Website"];
         
                list.Add(hospital);
            }
            return list;
        }

        public  int GetHospitalInfoCount(string where)
        {
            int count = 0;
            StringBuilder str = new StringBuilder();
            str.Append(" select count(*) from ");
            str.Append(tableName);
            if (where != null)
            {
                str.Append(" where ");
                str.Append(where);
                count = (int)DbHelperSQL.GetSingle(str.ToString());
            }
            else
            {
                count = (int)DbHelperSQL.GetSingle(str.ToString());
            }
            return count;
        }

        #region IHospitalInfoDAL 成员


        public List<HospitalInfo> GetHospitalInfos()
        {
            String sql = " select * from System_hospitalInfo where Hospital_State='1' ";
            return DataSetToList(DbHelperSQL.Query(sql));
            //throw new NotImplementedException();
        }

        public HospitalInfo GetHospitalInfoByID(string HospitalInfoID)
        {
            StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 Hospital_ID,City_ID,Hospital_No,Hospital_Name,Hospital_Level,Hospital_Nature,Hospital_Type,Hospital_Address,Hospital_Phone,Hospital_Website,Hospital_Introduce,Hospital_Logo,Hospital_AddTime,Hospital_Recoder,Hospital_LastAmendTime,Hospital_LastAmendPerson,Hospital_State from System_hospitalInfo ");
			strSql.Append(" where Hospital_ID=@Hospital_ID ");
			SqlParameter[] parameters = {
					new SqlParameter("@Hospital_ID", SqlDbType.Char,5)};
			parameters[0].Value = HospitalInfoID;

            HospitalInfo model = new HospitalInfo();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["Hospital_ID"]!=null)
				{
				model.Hospital_ID=ds.Tables[0].Rows[0]["Hospital_ID"].ToString();
				}
				if(ds.Tables[0].Rows[0]["City_ID"]!=null)
				{
				model.City_ID=ds.Tables[0].Rows[0]["City_ID"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Hospital_No"]!=null)
				{
				model.Hospital_No=ds.Tables[0].Rows[0]["Hospital_No"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Hospital_Name"]!=null)
				{
				model.Hospital_Name=ds.Tables[0].Rows[0]["Hospital_Name"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Hospital_Level"]!=null)
				{
				model.Hospital_Level=ds.Tables[0].Rows[0]["Hospital_Level"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Hospital_Nature"]!=null)
				{
				model.Hospital_Nature=ds.Tables[0].Rows[0]["Hospital_Nature"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Hospital_Type"]!=null)
				{
				model.Hospital_Type=ds.Tables[0].Rows[0]["Hospital_Type"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Hospital_Address"]!=null)
				{
				model.Hospital_Address=ds.Tables[0].Rows[0]["Hospital_Address"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Hospital_Phone"]!=null)
				{
				model.Hospital_Phone=ds.Tables[0].Rows[0]["Hospital_Phone"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Hospital_Website"]!=null)
				{
				model.Hospital_Website=ds.Tables[0].Rows[0]["Hospital_Website"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Hospital_Introduce"]!=null)
				{
				model.Hospital_Introduce=ds.Tables[0].Rows[0]["Hospital_Introduce"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Hospital_Logo"]!=null)
				{
				model.Hospital_Logo=ds.Tables[0].Rows[0]["Hospital_Logo"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Hospital_AddTime"].ToString()!="")
				{
					model.Hospital_AddTime=DateTime.Parse(ds.Tables[0].Rows[0]["Hospital_AddTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Hospital_Recoder"]!=null)
				{
				model.Hospital_Recoder=ds.Tables[0].Rows[0]["Hospital_Recoder"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Hospital_LastAmendTime"].ToString()!="")
				{
					model.Hospital_LastAmendTime=DateTime.Parse(ds.Tables[0].Rows[0]["Hospital_LastAmendTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Hospital_LastAmendPerson"]!=null)
				{
				model.Hospital_LastAmendPerson=ds.Tables[0].Rows[0]["Hospital_LastAmendPerson"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Hospital_State"]!=null)
				{
				model.Hospital_State=ds.Tables[0].Rows[0]["Hospital_State"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		
        
          //  throw new NotImplementedException();
        }

        #endregion
    }
}
